﻿#region Licence
// <copyright file="Helpers/ColorsHelper.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using System.Windows.Media;

#endregion

namespace Helpers
{
    /// <summary>
    /// Helper sur le type <see cref="Color"/> et valeurs hexadécimales
    /// </summary>
    public static class ColorsHelper
    {
        /// <summary>
        /// Transforme un Color en valeur hexadécimale
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ColorToHex(Color color)
        {
            return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
        }

        /// <summary>
        /// Transforme un Color en valeur hexadécimale en indiquant une transparence
        /// </summary>
        /// <param name="color"></param>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static string ColorToHexWithAlpha(Color color, int alpha = 255)
        {
            return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", alpha, color.R, color.G, color.B);
        }

        /// <summary>
        /// Transforme une valeur hexadécimale en Color
        /// </summary>
        /// <param name="hexValue"></param>
        /// <returns></returns>
        public static Color HexToColor(string hexValue)
        {
            try
            {
                if (!string.IsNullOrEmpty(hexValue))
                {
                    var convertFromString = ColorConverter.ConvertFromString(hexValue);
                    if (convertFromString != null)
                    {
                        return (Color)convertFromString;    
                    }
                }
                return Colors.Transparent;
            }
            catch (Exception)
            {
                return Colors.Transparent;
            }
        }

        /// <summary>
        /// Permet de transformerune couleur en double pour les applications Office
        /// </summary>
        /// <param name="color">Couelur à transformer</param>
        /// <returns>Couleur au formart double pour Office</returns>
        public static double ColorToColorForOffice(Color color)
        {
            uint returnValue = color.B;
            returnValue = returnValue << 8;
            returnValue += color.G;
            returnValue = returnValue << 8;
            returnValue += color.R;
            return returnValue;
        }
    }
}
